//
//  SongerDataViewController.m
//  NewMusic
//
//  Created by chuxinyu123 on 15/11/20.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "SongerDataViewController.h"
#import "NavigationBarBackImageView.h"
#import "ClassifyButtonView.h"
#import "CustomerNetworking.h"
#import <UIImageView+WebCache.h>
#import <SDWebImageManager.h>
#import "HotListenListViewController.h"
#import "AVPlayerViewController.h"

#import "FirstCollectionViewCell.h"
#import "SecondCollectionViewCell.h"
#import "TiredCollectionViewCell.h"
#import "FourthCollectionViewCell.h"
#import "BaseClassNavigationController.h"
@interface SongerDataViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate, UIScrollViewDelegate, classifyButtonViewDelegate, CXYFirstCollectionDelegate, SecondCollectionViewCellDelegate, TiredCollectionViewDelegate, fourthCollectionViewCellDelegate>

@property(nonatomic, retain) UICollectionView *collectionView;
@property(nonatomic, copy) NSString *nameString;
@property(nonatomic, retain) NSMutableArray *refsArray;
@property(nonatomic, retain) NSMutableArray *buttonArray;
@property(nonatomic, retain) UIScrollView *scrollView;
//图片
@property(nonatomic, retain) UIImageView *picImageView;
//标题
@property(nonatomic, retain) UILabel *nameLabel;

@property(nonatomic, assign) CGFloat contenOffSizeY;
@property(nonatomic, retain) ClassifyButtonView *classifyButtonView;
@property(nonatomic, retain) UINavigationBar *bar;
@property(nonatomic, retain) UIImageView *backImage;
@property(nonatomic, retain) NavigationBarBackImageView *barBackImgeView;
@end

@implementation SongerDataViewController

 

-(void)dealloc {
    [_nameLabel release];
    [_buttonArray release];
    [_refsArray release];
    [_picImageView release];
    [_bar release];
    [_backImage release];
    [_barBackImgeView release];
    [_classifyButtonView release];
    [_scrollView release];
    [_collectionView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
 // Do any additional setup after loading the view.
    self.refsArray = [NSMutableArray array];
    self.buttonArray = [NSMutableArray array];

 //创建视图;
  //  [self createSubView];
 //自定义navigationBar
//    [self createTabbar];
    //创建collectionView
    //[self createCollectionView];
 //获取数据
    //[self loadData];
}
 #pragma mark 自定义navigationBar
-(void)createTabbar {
    self.bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 375, 64)];
    [self.view addSubview:self.bar];
    //navigationbar背景图片
    self.barBackImgeView = [[NavigationBarBackImageView alloc]initWithFrame:CGRectMake(0, 0, self.bar.frame.size.width, self.bar.frame.size.height)];
    _barBackImgeView.alpha = 0;
    [_bar addSubview:_barBackImgeView];
    [_barBackImgeView release];
    self.bar.tintColor = [UIColor whiteColor];
    [self.bar setBackgroundImage:[UIImage imageNamed:@"nav.jpg"] forBarMetrics:UIBarMetricsCompact];
    [[self findHairlineImageViewUnder:self.bar] setHidden:YES];
    UIBarButtonItem *itemBar = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(retutnAction:)];
    UINavigationItem *item = [[UINavigationItem alloc] init];
    [self.bar pushNavigationItem:item animated:YES];
    [item setLeftBarButtonItem:itemBar];
    [itemBar release];
    [_bar release];
    
}

//id
- (void)setIDString:(NSString *)IDString
{
    if (_IDString != IDString) {
        [_IDString release];
        _IDString = [IDString retain];
    }
    [self createCollectionView];
    [self createSubView];
    [self createTabbar];
}

-(void)retutnAction:(UIButton *)button {
 
    [self.navigationController popViewControllerAnimated:YES];
 
    [self.navigationController setNavigationBarHidden:NO animated:YES];
 
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

#pragma mark 创建视图

-(void)createSubView {
//图片
    self.picImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 230)];
    _picImageView.backgroundColor = [UIColor redColor];
    [_picImageView sd_setImageWithURL:[NSURL URLWithString:_pic_Url]];
    _picImageView.userInteractionEnabled = YES;
    [self.view addSubview:_picImageView];
    [_picImageView release];
//标题
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, self.picImageView.frame.size.height / 5 * 3, self.picImageView.frame.size.width / 2, 30)];
    self.nameLabel.text = _singerName;
    _nameLabel.textColor = [UIColor whiteColor];
    _nameLabel.font = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:17];
 //    _nameLabel.backgroundColor = [UIColor purpleColor];
    [self.picImageView addSubview:_nameLabel];
    [_nameLabel release];
//分类视图
    NSMutableArray *buttonArray = [NSMutableArray arrayWithObjects:@"单曲", @"专辑", @"MV", @"歌手信息", nil];
    self.classifyButtonView = [[ClassifyButtonView alloc]initWithFrame:CGRectMake(0,  self.picImageView.frame.size.height - 40, self.picImageView.frame.size.width, 40)];
    _classifyButtonView.delegate = self;
    _classifyButtonView.titleArray = buttonArray;
    [self.picImageView addSubview:_classifyButtonView];
    [_classifyButtonView release];
}

#pragma mark 创建collectionView
-(void)createCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height ) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    self.contenOffSizeY = _collectionView.contentOffset.x;
//注册cell的重用标识
    [_collectionView registerClass:[FirstCollectionViewCell class] forCellWithReuseIdentifier:@"firstCollectionViewCell"];
    [_collectionView registerClass:[SecondCollectionViewCell class] forCellWithReuseIdentifier:@"secondCollectionViewCell"];
    [_collectionView registerClass:[TiredCollectionViewCell class] forCellWithReuseIdentifier:@"tiredCollectionViewCell"];
    [_collectionView registerClass:[FourthCollectionViewCell class]forCellWithReuseIdentifier:@"fourthCollectionViewCell"];
    [self.view addSubview:_collectionView];
    [flowLayout release];
    [_collectionView release];
}
#pragma mark cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        FirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"firstCollectionViewCell" forIndexPath:indexPath];
        cell.IDString = _IDString;
        cell.delegate = self;
        return cell;
    }else if (indexPath.row == 1) {
        SecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"secondCollectionViewCell" forIndexPath:indexPath];
        cell.IDString = _IDString;
        cell.delegate = self;
        return cell;
    }else if (indexPath.row == 2) {
        TiredCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tiredCollectionViewCell" forIndexPath:indexPath];
        cell.delegate = self;
        cell.IDString = _IDString;
        return cell;
    }else if (indexPath.row == 3){
        FourthCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"fourthCollectionViewCell" forIndexPath:indexPath];
        cell.IDString = _IDString;
        cell.delegate = self;
        return cell;
    }else
    {
        return nil;
    }
}



#pragma mark cell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
#pragma mark cell的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
}
#pragma mark cell的最小间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
 
}
#pragma mark cell的最小行间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

//带跳转MV
- (void)pushMV:(NSString *)string andIDString:(NSString *)idString
{
    AVPlayerViewController *player = [AVPlayerViewController new];
    BaseClassNavigationController *navgationController = [[BaseClassNavigationController alloc] initWithRootViewController:player];
    [navgationController.navigationBar setBackgroundImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"nav" ofType:@"jpg"]] forBarMetrics:UIBarMetricsCompact];
    player.urlString = string;
    player.idString = idString;
    [self presentViewController:navgationController animated:YES completion:^{
        
    }];
}

- (void)pushSecondController:(NSString *)string
{
    HotListenListViewController *hot = [[HotListenListViewController alloc] init];
    hot.idString = string;
    hot.numberString = @"http://api.dongting.com/song/album/";
    [self.navigationController pushViewController:hot animated:YES];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [hot release];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat num = scrollView.contentOffset.x / self.view.frame.size.width;
    _collectionView.frame = CGRectMake(0, 0, _collectionView.frame.size.width, _collectionView.frame.size.height);
    _picImageView.frame = CGRectMake(0, 0, _picImageView.frame.size.width, _picImageView.frame.size.height);
    _classifyButtonView.frame = CGRectMake(0, _picImageView.frame.size.height - 40, _classifyButtonView.frame.size.width, _classifyButtonView.frame.size.height);
    _barBackImgeView.alpha = 0;
    self.collectionView.contentOffset = CGPointMake(0, 250);
        _classifyButtonView.buttonTag = num + 2500;
    self.contenOffSizeY = scrollView.contentOffset.x;
}
#pragma mark classifyButtonView的代理方法
-(void)buttonClickAction:(UIButton *)button {
    self.collectionView.contentOffset = CGPointMake(self.view.frame.size.width * (button.tag - 2500), 0);
    if (_refsArray.count != 0) {
        CATransition *anim = [CATransition animation];
        anim.type = @"fade"; // 动画过渡类型
        anim.subtype = kCATransitionFromRight; // 动画过渡方向
        anim.duration = 0.5; // 动画持续1s
// 代理，动画执行完毕后会调用delegate的animationDidStop:finished:
        anim.delegate = self;
        [self.picImageView.layer addAnimation:anim forKey:@"layer"];
    }
 }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}


/*
 - (void)FirstTableViewScrollFrame:(CGFloat)height tableViewContentOffY:(CGFloat)tableViewContentOffY top:(BOOL)top
 {
 if (top == YES) {
 self.picImageView.frame = CGRectMake(0, self.picImageView.frame.origin.y - height, self.view.frame.size.width, self.picImageView.frame.size.height);
 _classifyButtonView.backView.alpha = tableViewContentOffY / 146;
 if (_classifyButtonView.backView.alpha > 1) {
 _classifyButtonView.backView.alpha =  1;
 }else if (_classifyButtonView.backView.alpha < 0.7) {
 _classifyButtonView.backView.alpha =  0.7;
 }
 if (tableViewContentOffY > 146) {
 _classifyButtonView.frame = CGRectMake(0, 64, self.view.frame.size.width, 40);
 [self.view addSubview:_classifyButtonView];
 }
 _barBackImgeView.alpha = tableViewContentOffY / 146;
 }else {
 self.picImageView.frame = CGRectMake(0, self.picImageView.frame.origin.y - height, self.view.frame.size.width, self.picImageView.frame.size.height);
 _classifyButtonView.backView.alpha =  tableViewContentOffY / 146;
 if (_classifyButtonView.backView.alpha > 1) {
 _classifyButtonView.backView.alpha =  1;
 }else if (_classifyButtonView.backView.alpha < 0.7) {
 _classifyButtonView.backView.alpha =  0.7;
 }
 if (tableViewContentOffY < 146) {
 _classifyButtonView.frame = CGRectMake(0, _picImageView.frame.size.height - 40, self.view.frame.size.width, 40);
 [self.picImageView addSubview:_classifyButtonView];
 }
 _barBackImgeView.alpha = tableViewContentOffY / 146;
 }
 }
 
 - (void)SecondTableViewScrollFrame:(CGFloat)height tableViewContentOffY:(CGFloat)tableViewContentOffY top:(BOOL)top
 {
 if (top == YES) {
 self.picImageView.frame = CGRectMake(0, self.picImageView.frame.origin.y - height, self.view.frame.size.width, self.picImageView.frame.size.height);
 _classifyButtonView.backView.alpha = tableViewContentOffY / 146;
 if (_classifyButtonView.backView.alpha > 1) {
 _classifyButtonView.backView.alpha =  1;
 }else if (_classifyButtonView.backView.alpha < 0.7) {
 _classifyButtonView.backView.alpha =  0.7;
 }
 if (tableViewContentOffY > 146) {
 _classifyButtonView.frame = CGRectMake(0, 64, self.view.frame.size.width, 40);
 [self.view addSubview:_classifyButtonView];
 }
 _barBackImgeView.alpha = tableViewContentOffY / 146;
 }else {
 self.picImageView.frame = CGRectMake(0, self.picImageView.frame.origin.y - height, self.view.frame.size.width, self.picImageView.frame.size.height);
 _classifyButtonView.backView.alpha =  tableViewContentOffY / 146;
 if (_classifyButtonView.backView.alpha > 1) {
 _classifyButtonView.backView.alpha =  1;
 }else if (_classifyButtonView.backView.alpha < 0.7) {
 _classifyButtonView.backView.alpha =  0.7;
 }
 if (tableViewContentOffY < 146) {
 _classifyButtonView.frame = CGRectMake(0, _picImageView.frame.size.height - 40, self.view.frame.size.width, 40);
 [self.picImageView addSubview:_classifyButtonView];
 }
 _barBackImgeView.alpha = tableViewContentOffY / 146;
 }
 }
 
 - (void)ThirdTableViewScrollFrame:(CGFloat)height tableViewContentOffY:(CGFloat)tableViewContentOffY top:(BOOL)top
 {
 if (top == YES) {
 self.picImageView.frame = CGRectMake(0, self.picImageView.frame.origin.y - height, self.view.frame.size.width, self.picImageView.frame.size.height);
 _classifyButtonView.backView.alpha = tableViewContentOffY / 146;
 if (_classifyButtonView.backView.alpha > 1) {
 _classifyButtonView.backView.alpha =  1;
 }else if (_classifyButtonView.backView.alpha < 0.7) {
 _classifyButtonView.backView.alpha =  0.7;
 }
 if (tableViewContentOffY > 146) {
 _classifyButtonView.frame = CGRectMake(0, 64, self.view.frame.size.width, 40);
 [self.view addSubview:_classifyButtonView];
 }
 _barBackImgeView.alpha = tableViewContentOffY / 146;
 }else {
 self.picImageView.frame = CGRectMake(0, self.picImageView.frame.origin.y - height, self.view.frame.size.width, self.picImageView.frame.size.height);
 _classifyButtonView.backView.alpha =  tableViewContentOffY / 146;
 if (_classifyButtonView.backView.alpha > 1) {
 _classifyButtonView.backView.alpha =  1;
 }else if (_classifyButtonView.backView.alpha < 0.7) {
 _classifyButtonView.backView.alpha =  0.7;
 }
 if (tableViewContentOffY < 146) {
 _classifyButtonView.frame = CGRectMake(0, _picImageView.frame.size.height - 40, self.view.frame.size.width, 40);
 [self.picImageView addSubview:_classifyButtonView];
 }
 _barBackImgeView.alpha = tableViewContentOffY / 146;
 }
 }
 
 - (void)FourthTableViewScrollFrame:(CGFloat)height tableViewContentOffY:(CGFloat)tableViewContentOffY top:(BOOL)top
 {
 if (top == YES) {
 self.picImageView.frame = CGRectMake(0, self.picImageView.frame.origin.y - height, self.view.frame.size.width, self.picImageView.frame.size.height);
 _classifyButtonView.backView.alpha = tableViewContentOffY / 146;
 if (_classifyButtonView.backView.alpha > 1) {
 _classifyButtonView.backView.alpha =  1;
 }else if (_classifyButtonView.backView.alpha < 0.7) {
 _classifyButtonView.backView.alpha =  0.7;
 }
 if (tableViewContentOffY > 146) {
 _classifyButtonView.frame = CGRectMake(0, 64, self.view.frame.size.width, 40);
 [self.view addSubview:_classifyButtonView];
 }
 _barBackImgeView.alpha = tableViewContentOffY / 146;
 }else {
 self.picImageView.frame = CGRectMake(0, self.picImageView.frame.origin.y - height, self.view.frame.size.width, self.picImageView.frame.size.height);
 _classifyButtonView.backView.alpha =  tableViewContentOffY / 146;
 if (_classifyButtonView.backView.alpha > 1) {
 _classifyButtonView.backView.alpha =  1;
 }else if (_classifyButtonView.backView.alpha < 0.7) {
 _classifyButtonView.backView.alpha =  0.7;
 }
 if (tableViewContentOffY < 146) {
 _classifyButtonView.frame = CGRectMake(0, _picImageView.frame.size.height - 40, self.view.frame.size.width, 40);
 [self.picImageView addSubview:_classifyButtonView];
 }
 _barBackImgeView.alpha = tableViewContentOffY / 146;
 }
 }
 */

@end

